<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Group extends Auth_Controller {

    public function __construct() {
        parent::__construct();
        if($_SESSION['user']['status'] != 1){
            redirect(site_url('home'));
        }
        $this->load->model('group_mdl');
    }

    public function index(){
        $this->load->library('pagination');
        $config['base_url'] = site_url('article/index');
        $config['total_rows'] = $this->group_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';

        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();

        $per_page = $this->input->get('per_page');
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->order_by('condition desc');
        $data['list'] = $this->group_mdl->select();

        $this->_view('index',$data);
    }

    /**
     * 权限删除
     */
    public function delete(){
        if(!$this->input->is_ajax_request()){
            show_error('参数错误！');
        }

        $gid = $this->input->post('gid');
        $gid = intval($gid);

        /**
         * 查询最小金额的权限
         */
        $this->db->select_min('condition');
        $min = $this->group_mdl->get_one();
        $this->db->where('condition',$min['condition']);
        $min = $this->group_mdl->get_one();

        $this->db->trans_start();
        $this->load->model('users_mdl');
        $this->db->where('group_id',$gid);
        $this->users_mdl->update(array('group_id'=>$min['id']));

        $this->db->where('id',$gid);
        $res = $this->group_mdl->delete();
        $this->db->trans_complete();

        echo $res === true ? 1 : '操作失败！';
    }

    /**
     * 新增权限
     */
    public function create(){
        if($this->input->is_ajax_request()){
            $post = $this->input->post();

            if(empty($post['title'])){
                die('权限名称不能为空');
            }
            // if(intval($post['discount']) < 0){
            //     die('折扣填写错误！ 如9.8折，只填写9.8即可');
            // }
            if(empty(intval($post['condition']))){
                die('升级金额填写错误！请填写大于0的正整数');
            }

            $data = array(
                'title' => $post['title'],
                'condition' => intval($post['condition']),
                'remark' => $post['remark'],
                'update_time' => time()
            );

            $res = $this->group_mdl->create($data);

            echo $res == true ? 1 : '操作失败！';
        }else{
            $this->_view('create');
        }
    }

    /**
     * 权限更改
     */
    public function update(){

        if($this->input->is_ajax_request()){
            $post = $this->input->post();

            if(empty($post['title'])){
                die('权限名称不能为空');
            }
            // if(intval($post['discount']) < 0){
            //     die('折扣填写错误！ 如9.8折，只填写9.8即可');
            // }
            if(empty(intval($post['condition']))){
                die('升级金额填写错误！请填写大于0的正整数');
            }

            $data = array(
                'title' => $post['title'],
                //'discount' => $post['discount'],
                'condition' => intval($post['condition']),
                'remark' => $post['remark'],
                'update_time' => time()
            );

            $this->db->where('id',intval($post['gid']));
            $res = $this->group_mdl->update($data);
            echo $res === true ? '1' : '操作失败';
        }else{
            $gid = $this->input->get('gid');
            $gid = intval($gid);
            if(empty($gid)){
                show_error('参数错误！');
            }

            $this->db->where('id',$gid);
            $res = $this->group_mdl->get_one();
            $this->_view('update',array('res'=>$res));
        }
    }

}

/* End of file Group.php */
/* Location: ./application/controllers/Group.php */